package com.example.springsecurityoauthdemo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springsecurityoauthdemo.entity.SysUser;
import com.example.springsecurityoauthdemo.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.stereotype.Service;

import java.util.List;

//@Service("userDetailsService")
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        SysUser one = iSysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, s));
        if (one==null){
            //throw new AuthenticationCredentialsNotFoundException("无当前用户");
            throw new UsernameNotFoundException("无当前用户");
        }
        List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList("update");
        return new User(one.getUsername(),one.getPassword(),list);


    }

}
